/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-07-29 16:26:08
 * @LastEditors: zt 2993721164@qq.com
 * @LastEditTime: 2025-07-31 17:13:37
 * @FilePath: /yyshort/lib/common/apis/goods_api.dart
 * @Description: 商品相关API接口
 */
import 'package:yyshort/common/apis/apis.dart';
import 'package:yyshort/common/model/goods_model.dart';
import 'package:yyshort/common/model/video_model.dart';
import 'package:yyshort/common/utils/http.dart';

class GoodsAPI {
  /// @description: 获取充值档位列表
  /// @return {*} RechargeListModel.list
  static Future get_recharge_list() async {
    var response = await HttpUtil().post('/get_recharge_list');
    GoodsListModel rechargeListModel = GoodsListModel.fromJson(response);
    return rechargeListModel.list;
  }

  /// @description: 创建订单
  /// @param {String} gearId 档位ID（必填）
  /// @param {String} payType 支付类型：1-google 2-apple（必填）
  /// @return {*} OrderModel
  static Future create_order(
    String gearId,
    String payType, {
    EpisodeModel? episodeModel,
  }) async {
    // 构建form-data参数
    Map<String, dynamic> formData;
    if (episodeModel != null) {
      formData = {
        'gear_id': gearId,
        'pay_type': payType,
        'video_id': episodeModel.videoId.toString(),
        'episode_id': episodeModel.id.toString(),
      };
    } else {
      formData = {'gear_id': gearId, 'pay_type': payType};
    }

    Map<String, dynamic> response = await HttpUtil().post(
      '/create_order',
      data: formData,
    );
    // OrderModel orderModel = OrderModel.fromJson(response);
    // return orderModel;
    int order_id = response['order_id'];
    return order_id.toString();
  }

  /// @description: 苹果支付订单验证
  /// @param {String} transactionId 苹果交易ID（必填）
  /// @param {String} order_id 订单号ID（必填）
  /// @return {*} Map 包含code和msg的响应
  static Future<bool> apple_pay_verify_order(
    String transactionId,
    String order_id,
  ) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {
      'transaction_id': transactionId,
      'order_id': order_id,
    };
    Map<String, dynamic> response = await HttpUtil().post(
      '/apple_pay_verify_order',
      data: formData,
    );

    if (response['code'] is int && response['code'] == 1) {
      UserAPI.get_user_info();
      return true;
    } else {
      return false;
    }
  }

  /// @description: 苹果支付补单
  /// @param {String} transactionId 苹果交易ID（必填）
  /// @return {*} Map 包含code和msg的响应
  static Future<bool> apple_pay_supplement_order(String transactionId) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {'transaction_id': transactionId};
    Map<String, dynamic> response = await HttpUtil().post(
      '/apple_pay_supplement_order',
      data: formData,
    );

    if (response['code'] is int && response['code'] == 1) {
      return true;
    } else {
      return false;
    }
  }

  /// @description: google支付订单验证
  /// @return {*} Map 包含code和msg的响应
  static Future<bool> google_pay_verify_order(
    String order_id,
    String google_order_id,
    String product_id,
    String purchase_token,
  ) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {
      'order_id': order_id,
      'google_order_id': google_order_id,
      'product_id': product_id,
      'purchase_token': purchase_token,
    };
    Map<String, dynamic> response = await HttpUtil().post(
      '/google_pay_verify_order',
      data: formData,
    );

    if (response['code'] is int && response['code'] == 1) {
      UserAPI.get_user_info();
      return true;
    } else {
      return false;
    }
  }
}
